package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.field.UpLoadFileListener;

import com.chijun.mform.full.mode.ModeFormFile;

import com.chijun.mform.full.mode.ModeFormFullFileData;
import com.example.ccj.b.c.custom.LoadImageControlImageListener;
import com.example.ccj.b.c.custom.SelectPictureMode;
import com.example.ccj.b.c.custom.TransverseLoadImageControl;
import com.example.ccj.b.c.custom.image.IImageChangeDelete;
import com.example.ccj.b.c.custom.image.IImageChangeUpload;
import com.example.ccj.b.c.custom.image.ImageChangeListener;
import com.example.ccj.b.c.custom.image.LoadImagesRecycler;
import com.example.ccj.b.c.custom.image.MLocalMedia;
import com.example.ccj.u.sqlite.SqliteExcuteCommand;
import com.example.ccj.u.tengxun.cos.COS;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.ObservableEmitter;

/**
 * 说明文字
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:43
 */
public class FieldFor10TuPian extends BaseField {
    public FieldFor10TuPian(){
        super();
        localMediaList = new ArrayList<>();
        uploadedImg = new ArrayList<>();
        formFileModeList = new ArrayList<>();
    }
    private List<LocalMedia> localMediaList;            //选择的图片
    private List<LocalMedia> uploadedImg;




    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;

        writeFormClass.fieldFullContentView.setVisibility(View.GONE);

        TransverseLoadImageControl loadImageControl = new TransverseLoadImageControl(context,
                true, formField.getLittleNum(), SelectPictureMode.pickMultiplePicture, new LoadImageControlImageListener() {
            @Override
            public void onIncrease(List<LocalMedia> list) {
                List<LocalMedia> chaJi = new ArrayList<>();
                for (LocalMedia localMedia : list) {
                    if (!uploadedImg.contains(localMedia)) {
                        chaJi.add(localMedia);
                    }
                }

                writeFormClass.functionSaveImage(context, chaJi, new UpLoadFileListener() {
                    @Override
                    public void onComplete(ModeFormFile formImageMode, File file, LocalMedia localMedia) {
                        localMediaList.add(localMedia);
                        uploadedImg.add(localMedia);
                        formFileModeList.add(formImageMode);
                    }
                });
            }
            @Override
            public void onDelete(LocalMedia picture, int position) {
                if (formFileModeMap.containsKey(picture.getFileName())) {
                    ModeFormFile fileMode = formFileModeMap.get(picture.getFileName());
                    if (fileMode != null) {
                        String url = fileMode.url;
                        functionDeleteImg(context, url, new CosFileDeleteListener() {
                            @Override
                            public void onSuccess(String path) {
                                // TODO: 2024/12/24 删除成功
                            }

                            @Override
                            public void onSuccess(ModeFormFile formFileMode) {
                                // TODO: 2024/12/24 删除成功
                            }

                            @Override
                            public void onError(String error) {
                                // TODO: 2024/12/24 删除失败
                            }
                        });
                    }
                }
            }
        });

        writeFormClass.fieldFullContentLayout.addView(loadImageControl);

        //加载暂存图片
        List<ModeFormFile> formFileModeList1 = SqliteExcuteCommand.get(context, ModeFormFile.class, "sanJiId=?", String.valueOf(formField.getId()));
        if (formFileModeList1 != null) {
            this.formFileModeList = formFileModeList1;
            if (localMediaList != null) {
                List<LocalMedia> list = new ArrayList<>();
                for (ModeFormFile formFileMode : formFileModeList) {
                    list.add(LocalMedia.generateLocalMedia(context,formFileMode.path));
                }
                uploadedImg.addAll(list);
                localMediaList.addAll(list);
            }
            loadImageControl.setPictureList(localMediaList);
        }

        return writeFormClass.view;
    }
    

    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.goneFieldNameView();
        //隐藏值字段
        fullModel.goneContentView();

        LoadImagesRecycler loadImagesRecycler = new LoadImagesRecycler(fullModel.getContext());
        loadImagesRecycler.getTitleView().getTvTitleView().setTextSize(fullModel.titleTextSize);

        if (fullModel.isHaveModifyPower()) {
            loadImagesRecycler.getTitleView().getTvLeftTag().setBackgroundColor(fullModel.localConfig.field_have_modify_power_left_tag_color);

            loadImagesRecycler.setMode(LoadImagesRecycler.NETWORK_AND_LOCAL_NO_UPLOAD);
            loadImagesRecycler.setImageChangeListener(new ImageChangeListener() {
                @Override
                public void imageNetWorkDelete(MLocalMedia images, IImageChangeDelete iImageChangeDelete) {
                    //审批过程删除的文件放入数组，提交审批时统一删除
                    ModeFormFullFileData formFullFileData =
                            getFileFullMode(context, "form"
                                    , new File(images.getRealPath())
                                    , fullModel.serverFullData.launchedForm.formId
                                    , fullModel.serverFullData.launchedForm.id
                                    , fullModel.formField.getId());
                    processDeleteFormFullFileDataList.add(formFullFileData);
                }
                @Override
                public void imageInsertNetWork(List<MLocalMedia> nowImages, IImageChangeUpload iImageChangeUpload) {
                    //审批过程添加的文件放入数组，提交审批时统一添加
                    for (MLocalMedia nowImage : nowImages) {
                        ModeFormFullFileData formFullFileData =
                                getFileFullMode(context, "form"
                                        , new File(nowImage.getRealPath())
                                        , fullModel.serverFullData.launchedForm.formId
                                        , fullModel.serverFullData.launchedForm.id
                                        , fullModel.formField.getId());
                        processAddFormFullFileDataList.add(formFullFileData);
                    }
                }
            });
        } else {
            loadImagesRecycler.getTitleView().getTvLeftTag().setBackgroundColor(fullModel.localConfig.leftColor);
            loadImagesRecycler.setMode(LoadImagesRecycler.NETWORK);
        }

        loadImagesRecycler.setMastShoot(fullModel.formField.isMastShoot());
        loadImagesRecycler.setMaxSum(fullModel.formField.getLittleNum());
        loadImagesRecycler.setTag(fullModel.formField.getHint());
        loadImagesRecycler.setTitleName(fullModel.formField.getName());
        List<ModeFormFullFileData> formFullFileDataList = fullModel.serverFullData.formFileModeMap.get(fullModel.formField.getId());
        List<MLocalMedia> mLocalMediaList = new ArrayList<>();
        if (formFullFileDataList != null) {
            for (ModeFormFullFileData formFullFileData : formFullFileDataList) {
                String fileUrl = COS.getInstance().getFileUrl(formFullFileData.bucket, formFullFileData.path);
                MLocalMedia mLocalMedia = MLocalMedia.generateHttpAsLocalMedia(fileUrl);
                mLocalMediaList.add(mLocalMedia);
            }
        }
        loadImagesRecycler.setData(mLocalMediaList);
        loadImagesRecycler.show();

        fullModel.addCustomControl(loadImagesRecycler);
        return fullModel.layout;
    }



    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getSelectNumView());     //最大数量
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getMastShootView());        //是否必须拍摄
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印


        return installClass.v;
    }
}
